package com.uenpay.im.service;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.qyx.android.message.type.MsgContentType;
import com.qyx.android.protocol.BaseContentModel;
import com.qyx.android.protocol.QyxMsg;
import com.uenpay.im.entities.chatbean.AttachModel;
import com.uenpay.im.entities.chatbean.MsgAudioModel;
import com.uenpay.im.entities.chatbean.MsgCancelCallVideoModel;
import com.uenpay.im.entities.chatbean.MsgCradModel;
import com.uenpay.im.entities.chatbean.MsgCrowdFundsModel;
import com.uenpay.im.entities.chatbean.MsgCrowdFundsSystemModel;
import com.uenpay.im.entities.chatbean.MsgFileModel;
import com.uenpay.im.entities.chatbean.MsgGifModel;
import com.uenpay.im.entities.chatbean.MsgImageModel;
import com.uenpay.im.entities.chatbean.MsgLocationModel;
import com.uenpay.im.entities.chatbean.MsgRedModel;
import com.uenpay.im.entities.chatbean.MsgRedSystemModel;
import com.uenpay.im.entities.chatbean.MsgRefuseVideoChatModel;
import com.uenpay.im.entities.chatbean.MsgRequestVideoChatModel;
import com.uenpay.im.entities.chatbean.MsgRevokeMsgModel;
import com.uenpay.im.entities.chatbean.MsgTextModel;
import com.uenpay.im.entities.chatbean.MsgTransferModel;
import com.uenpay.im.entities.chatbean.MsgVideoModel;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * @Author: SL
 * @Date: 2017/3/8 12:39
 * @CopyRight: http://www.qiyunxin.com
 * @Parameter: TODO
 * @Function: 消息实例和json数据的相互转换。发送消息时，发动的bean将转换为json数据保存在本地sqlite中。接受的或本地sqlite数据json将转换为bean实例呈现在会话中。
 */
public class JsonBeanTransf {

    /**
     * 发送的数据bean转为json
     *
     * @param mQyxMsg 消息对象
     * @return JSONObject
     */
    public static JSONObject msgBean2Json(QyxMsg mQyxMsg) {
        JSONObject jsonObject = new JSONObject();
        if (mQyxMsg != null && mQyxMsg.baseContentModel != null) {
            try {
                if (mQyxMsg.msg_content_type == MsgContentType.TEXT) {
                    MsgTextModel contentModel = (MsgTextModel) mQyxMsg.baseContentModel;
                    jsonObject.put("content", contentModel.getContent());
                } else if (mQyxMsg.msg_content_type == MsgContentType.PIC) {
                    MsgImageModel imageModel = (MsgImageModel) mQyxMsg.baseContentModel;
                    AttachModel attachModel = (AttachModel) imageModel
                            .getAttachmentModel();
                    if (attachModel != null) {
                        jsonObject.put("fileid", attachModel.getFileId());
                        jsonObject.put("filehash", attachModel.getFileHash());
                        jsonObject.put("height", attachModel.getPic_height());
                        jsonObject.put("width", attachModel.getPic_width());
                    }
                } else if (mQyxMsg.msg_content_type == MsgContentType.GIF_EXP) {
                    MsgGifModel msgGifModel = (MsgGifModel) mQyxMsg.baseContentModel;
                    jsonObject.put("content", msgGifModel.getGifName());
                    jsonObject.put("fileid", msgGifModel.getFileId());
                    jsonObject.put("filehash", msgGifModel.getFileHash());
                } else if (mQyxMsg.msg_content_type == MsgContentType.LOCATION) {
                    MsgLocationModel locationModel = (MsgLocationModel) mQyxMsg.baseContentModel;
                    jsonObject.put("latitude", locationModel.getLatitude());
                    jsonObject.put("longitude", locationModel.getLongitude());
                    jsonObject.put("content", locationModel.getContent());
                } else if (mQyxMsg.msg_content_type == MsgContentType.BUSINESS_CRAD) {
                    MsgCradModel cradModel = (MsgCradModel) mQyxMsg.baseContentModel;
                    jsonObject.put("cust_id", cradModel.getCustId());
                    jsonObject.put("cust_name", cradModel.getCustName());
                    jsonObject.put("crad_type", cradModel.getCradType());
                } else if (mQyxMsg.msg_content_type == MsgContentType.RED) {
                    MsgRedModel msgRedModel = (MsgRedModel) mQyxMsg.baseContentModel;
                    jsonObject.put("content", msgRedModel.getContent());
                    jsonObject.put("packetsid", msgRedModel.getPacketsid());
                } else if (mQyxMsg.msg_content_type == MsgContentType.FILE) {
                    MsgFileModel msgFileModel = (MsgFileModel) mQyxMsg.baseContentModel;
                    AttachModel attachModel = (AttachModel) msgFileModel
                            .getAttachmentModel();
                    if (attachModel != null) {
                        jsonObject.put("fileid", attachModel.getFileId());
                        jsonObject.put("filehash", attachModel.getFileHash());
                    }
                    jsonObject.put("name", msgFileModel.getName());
                    jsonObject.put("size", msgFileModel.getSize());
                } else if (mQyxMsg.msg_content_type == MsgContentType.AUDIO) {
                    MsgAudioModel audioModel = (MsgAudioModel) mQyxMsg.baseContentModel;
                    AttachModel attachModel = (AttachModel) audioModel
                            .getAttachmentModel();
                    if (attachModel != null) {
                        jsonObject.put("fileid", attachModel.getFileId());
                        jsonObject.put("filehash", attachModel.getFileHash());
                    }
                    jsonObject.put("second", audioModel.getSecond());
                } else if (mQyxMsg.msg_content_type == MsgContentType.CROWD_FUND) {
                    MsgCrowdFundsModel crowdFundsModel = (MsgCrowdFundsModel) mQyxMsg.baseContentModel;
                    jsonObject.put("amount", crowdFundsModel.getAmount());
                    jsonObject.put("fundid", crowdFundsModel.getFundId());
                    jsonObject.put("content", crowdFundsModel.getContent());
                } else if (mQyxMsg.msg_content_type == MsgContentType.VIDEO) {
                    MsgVideoModel msgVideoModel = (MsgVideoModel) mQyxMsg.baseContentModel;
                    AttachModel attachModel = (AttachModel) msgVideoModel
                            .getAttachmentModel();
                    if (attachModel != null) {
                        jsonObject.put("video_fileid", attachModel.getFileId());
                        jsonObject.put("video_filehash", attachModel.getFileHash());
                        jsonObject.put("cover_filehash", msgVideoModel.getScreeHostFileHash());
                        jsonObject.put("cover_fileid", msgVideoModel.getScreeHostFileId());
                        jsonObject.put("cover_width", msgVideoModel.getScreeHostW());
                        jsonObject.put("cover_height", msgVideoModel.getScreeHostH());
                        jsonObject.put("screenshot_path", msgVideoModel.getScreeHostLocalPath());
                    }
                } else if (mQyxMsg.msg_content_type == MsgContentType.REQUEST_VIDEO_CHAT) {
                    /** 视频通话请求 **/
                    MsgRequestVideoChatModel msgReqVideoChatModel = (MsgRequestVideoChatModel) mQyxMsg.baseContentModel;
                    jsonObject
                            .put("content", msgReqVideoChatModel.getContent());
                } else if (mQyxMsg.msg_content_type == MsgContentType.CANCEL_CALL_VIDEO_CHAT) {
                    /** 取消视频通话请求 **/
                    MsgCancelCallVideoModel msgCancelCallVideoModel = (MsgCancelCallVideoModel) mQyxMsg.baseContentModel;
                    jsonObject.put("content",
                            msgCancelCallVideoModel.getCancelCallVideoMsgNo());
                } else if (mQyxMsg.msg_content_type == MsgContentType.REFUSE_VIDEO_CHAT) {
                    /** 拒绝视频通话请求 **/
                    MsgRefuseVideoChatModel msgRefuseVideoChatModel = (MsgRefuseVideoChatModel) mQyxMsg.baseContentModel;
                    jsonObject.put("content",
                            msgRefuseVideoChatModel.getRefuseVideoChatMsgNo());
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return jsonObject;
    }

    /**
     * json数据转为消息bean（基类）
     * @param jsonObject json数据（来源：收到好友或群的消息，sqlite数据）
     * @param msgContentType
     * @return
     */
    public static BaseContentModel json2MsgBean(JSONObject jsonObject,int msgContentType){
        if (jsonObject == null)
            return null;
        if (msgContentType == MsgContentType.TEXT) {
            MsgTextModel sendTextModel = new MsgTextModel();
            sendTextModel.setContent(jsonObject.optString("content"));
            return sendTextModel;
        } else if (msgContentType == MsgContentType.PIC) {
            MsgImageModel imageModel = new MsgImageModel();
            AttachModel attachModel = new AttachModel();
            attachModel.setFileHash(jsonObject.optString("filehash"));
            attachModel.setFileId(jsonObject.optString("fileid"));
            attachModel.setPic_height(jsonObject.optString("height"));
            attachModel.setPic_width(jsonObject.optString("width"));
            imageModel.setAttachmentModel(attachModel);
            return imageModel;
        } else if (msgContentType == MsgContentType.GIF_EXP) {
            MsgGifModel msgGifModel = new MsgGifModel();
            msgGifModel.setGifName(jsonObject.optString("content"));
            if (jsonObject.has("filehash")) {
                msgGifModel.setFileHash(jsonObject.optString("filehash"));
            }
            if (jsonObject.has("fileid")) {
                msgGifModel.setFileId(jsonObject.optString("fileid"));
            }
            return msgGifModel;
        } else if (msgContentType == MsgContentType.LOCATION) {
            MsgLocationModel locationModel = new MsgLocationModel();
            locationModel.setLatitude(jsonObject.optString("latitude"));
            locationModel.setLongitude(jsonObject.optString("longitude"));
            return locationModel;
        } else if (msgContentType == MsgContentType.BUSINESS_CRAD) {
            MsgCradModel cradModel = new MsgCradModel();
            cradModel.setCustId(jsonObject.optString("cust_id"));
            cradModel.setCustName(jsonObject.optString("cust_name"));
            cradModel.setCradType(jsonObject.optInt("crad_type"));
            return cradModel;
        } else if (msgContentType == MsgContentType.RED) {
            MsgRedModel msgRedModel = new MsgRedModel();
            msgRedModel.setPacketsid(jsonObject.optString("packetsid"));
            msgRedModel.setContent(jsonObject.optString("content"));
            return msgRedModel;
        } else if (msgContentType == MsgContentType.FILE) {
            MsgFileModel msgFileModel = new MsgFileModel();
            AttachModel attachModel = new AttachModel();
            attachModel.setFileHash(jsonObject.optString("filehash"));
            attachModel.setFileId(jsonObject.optString("fileid"));
            msgFileModel.setName(jsonObject.optString("name"));
            msgFileModel.setSize(jsonObject.optString("size"));
            msgFileModel.setAttachmentModel(attachModel);
            return msgFileModel;
        } else if (msgContentType == MsgContentType.AUDIO) {
            MsgAudioModel audioModel = new MsgAudioModel();
            audioModel.setSecond(jsonObject.optInt("second"));
            AttachModel attachModel = new AttachModel();
            attachModel.setFileHash(jsonObject.optString("filehash"));
            attachModel.setFileId(jsonObject.optString("fileid"));
            audioModel.setAttachmentModel(attachModel);
            return audioModel;
        } else if (msgContentType == MsgContentType.RECEIVED_RED_SYSTEM_MSG) {
            /** 红包系统消息 **/
            MsgRedSystemModel msgRedSystemModel = new MsgRedSystemModel();
            msgRedSystemModel.setContent(jsonObject.optString("content"));
            return msgRedSystemModel;
        } else if (msgContentType == MsgContentType.TRANSFER_ACCOUNTS) {
            /** 转账 **/
            MsgTransferModel msgTransferModel = new MsgTransferModel();
            msgTransferModel.setAmount(jsonObject.optString("amount"));
            // msgTransferModel.setContent(jsonObject.optString("content"));
            msgTransferModel.setTurnoverid(jsonObject.optString("turnoverid"));
            msgTransferModel.setMemo(jsonObject.optString("memo"));
            return msgTransferModel;
        } else if (msgContentType == MsgContentType.CROWD_FUND) {
            /** 众筹 **/
            MsgCrowdFundsModel crowdFundsModel = new MsgCrowdFundsModel();
            crowdFundsModel.setAmount(jsonObject.optString("amount"));
            crowdFundsModel.setFundId(jsonObject.optString("fundid"));
            crowdFundsModel.setContent(jsonObject.optString("content"));
            return crowdFundsModel;
        } else if (msgContentType == MsgContentType.CROWD_FUND_SYSTEM_MSG) {
            /** 众筹系统消息 **/
            MsgCrowdFundsSystemModel fundsSystemModel = new MsgCrowdFundsSystemModel();
            fundsSystemModel.setFundId(jsonObject.optString("fundid"));
            fundsSystemModel.setContent(jsonObject.optString("content"));
            return fundsSystemModel;
        } else if (msgContentType == MsgContentType.VIDEO) {
            /**视频**/
            MsgVideoModel msgVideoModel = new MsgVideoModel();
            AttachModel attachModel = new AttachModel();
            attachModel.setFileId(jsonObject.optString("video_fileid"));
            attachModel.setFileHash(jsonObject.optString("video_filehash"));
            msgVideoModel.setScreeHostW(jsonObject.optInt("cover_width"));
            msgVideoModel.setScreeHostH(jsonObject.optInt("cover_height"));
            msgVideoModel.setScreeHostFileHash(jsonObject.optString("cover_filehash"));
            msgVideoModel.setScreeHostFileId(jsonObject.optString("cover_fileid"));
            if (jsonObject.has("screenshot_path"))
                msgVideoModel.setScreeHostLocalPath(jsonObject.optString("screenshot_path"));
            msgVideoModel.setAttachmentModel(attachModel);
            return msgVideoModel;
        }  else if (msgContentType == MsgContentType.REVOKE_MSG) {
            /** 撤回消息 **/
            MsgRevokeMsgModel revokeMsgModel = new MsgRevokeMsgModel();
            revokeMsgModel.setFromCustId(jsonObject.optString("from_cust_id"));
            return revokeMsgModel;
        } else if (msgContentType == MsgContentType.REQUEST_VIDEO_CHAT) {
            /** 视频通话请求 **/
            MsgRequestVideoChatModel msgRequestVideoChatModel = new MsgRequestVideoChatModel();
            msgRequestVideoChatModel
                    .setContent(jsonObject.optString("content"));
            if (jsonObject.has("video_chat_result")) {
                msgRequestVideoChatModel.setVideoChatResultType(jsonObject
                        .optInt("video_chat_result"));
            }
            return msgRequestVideoChatModel;
        } else if (msgContentType == MsgContentType.CANCEL_CALL_VIDEO_CHAT) {
            /** 取消视频通话请求 **/
            MsgCancelCallVideoModel msgCancelCallVideoModel = new MsgCancelCallVideoModel();
            msgCancelCallVideoModel.setCancelCallVideoMsgNo(jsonObject
                    .optString("content"));
            return msgCancelCallVideoModel;
        } else if (msgContentType == MsgContentType.REFUSE_VIDEO_CHAT) {
            /** 拒绝视频通话请求 **/
            MsgRefuseVideoChatModel msgRefuseVideoChatModel = new MsgRefuseVideoChatModel();
            msgRefuseVideoChatModel.setRefuseVideoChatMsgNo(jsonObject
                    .optString("content"));
            return msgRefuseVideoChatModel;
        } else {
            return null;
        }
    }

    /**
     * gson转消息bean
     * @param jsonObject
     * @param msgContentType
     * @return
     */
    public static BaseContentModel gson2MsgBean(JSONObject jsonObject,int msgContentType){
        Gson gson = new Gson();
        java.lang.reflect.Type type = null;
        if (msgContentType == MsgContentType.TEXT) {
            type = new TypeToken<MsgTextModel>() {
            }.getType();
            return gson.fromJson(jsonObject.toString(), type);
        } else if (msgContentType == MsgContentType.PIC) {
            type = new TypeToken<MsgImageModel>() {
            }.getType();
        } else if (msgContentType == MsgContentType.AUDIO) {
            type = new TypeToken<MsgAudioModel>() {
            }.getType();
        } else if (msgContentType == MsgContentType.LOCATION) {
            type = new TypeToken<MsgLocationModel>() {
            }.getType();
        } else if (msgContentType == MsgContentType.BUSINESS_CRAD) {
            type = new TypeToken<MsgCradModel>() {
            }.getType();
        } else if (msgContentType == MsgContentType.FILE) {
            type = new TypeToken<MsgFileModel>() {
            }.getType();
        } else if (msgContentType == MsgContentType.GIF_EXP) {
            type = new TypeToken<MsgGifModel>() {
            }.getType();
        } else if (msgContentType == MsgContentType.RED) {
            type = new TypeToken<MsgRedModel>() {
            }.getType();
        } else if (msgContentType == MsgContentType.TRANSFER_ACCOUNTS) {
            type = new TypeToken<MsgTransferModel>() {
            }.getType();
        } else if (msgContentType == MsgContentType.RECEIVED_RED_SYSTEM_MSG) {
            type = new TypeToken<MsgRedSystemModel>() {
            }.getType();
        } else if (msgContentType == MsgContentType.REQUEST_VIDEO_CHAT) {
            type = new TypeToken<MsgRequestVideoChatModel>() {
            }.getType();
        } else if (msgContentType == MsgContentType.CANCEL_CALL_VIDEO_CHAT) {
            type = new TypeToken<MsgCancelCallVideoModel>() {
            }.getType();
        } else if (msgContentType == MsgContentType.REFUSE_VIDEO_CHAT) {
            type = new TypeToken<MsgRefuseVideoChatModel>() {
            }.getType();
        } else if (msgContentType == MsgContentType.CROWD_FUND) {
            type = new TypeToken<MsgCrowdFundsModel>() {
            }.getType();
        } else if (msgContentType == MsgContentType.CROWD_FUND_SYSTEM_MSG) {
            type = new TypeToken<MsgCrowdFundsSystemModel>() {
            }.getType();
        } else if (msgContentType == MsgContentType.VIDEO) {
            type = new TypeToken<MsgVideoModel>() {
            }.getType();
        }
        if (type != null) {
            // 处理附件
            BaseContentModel baseContentModel = gson.fromJson(
                    jsonObject.toString(), type);
            if (baseContentModel.getAttachmentModel() != null) {
                if (jsonObject.has("attachmentModel")) {
                    String attachmentModel = jsonObject
                            .optString("attachmentModel");
                    AttachModel attachModel = gson.fromJson(attachmentModel,
                            new TypeToken<AttachModel>() {
                            }.getType());
                    baseContentModel.setAttachmentModel(attachModel);
                }
            }
            return baseContentModel;
        } else {
            return null;
        }
    }

}
